﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using Fishing.Model;
using MySql.Data.MySqlClient;
using NLog;

namespace Fishing.Dal
{
    /// <summary>
    ///  枚举表
    /// </summary>
    public class SSEnumDal
    {

        /// <summary>
        /// ConStr
        /// </summary>
        private static readonly string ConStr = ConfigurationManager.ConnectionStrings["_fishingdb"].ConnectionString;


        private static readonly Logger _logger = LogManager.GetCurrentClassLogger();

        /// <summary>
        /// 获取所有枚举类型
        /// </summary>
        /// <returns></returns>
        public List<ss_enum> GetSSEnum()
        {
            var retList = new List<ss_enum>();
            var sql = "select * from ss_enum where 1=1 ";

            //sql = sql + " order by `order` asc";

            var connection = new MySqlConnection(ConStr);
            var command = new MySqlCommand
            {
                Connection = connection,
                CommandText = sql,
                CommandType = CommandType.Text
            };
            try
            {
                connection.Open();
                using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            retList.Add(new ss_enum()
                            {
                                enumID = Convert.ToInt32(reader["enumID"]),
                                enum_Parent_ID = Convert.ToInt32(reader["enum_Parent_ID"]),
                                enum_Seq = Convert.ToInt32(reader["enum_Seq"]),
                                enum_Number = reader["enum_Number"].ToString(),
                                enumName = reader["enumName"].ToString(),
                                enumTitle = reader["enumTitle"].ToString()
                            });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error("======================================================");
                _logger.Error("GetSSEnum ERROR !");
                _logger.Error("ERROR Message : " + ex.Message);
                _logger.Error("ERROR StackTrace : " + ex.StackTrace);
                _logger.Error("======================================================");
            }
            finally
            {
                connection.Close();
            }
            return retList;

        }


        public List<ss_enum> GetMenuByuserID(int Parent, string userid)
        {
            var retList = new List<ss_enum>();
            string sel = " * ";
            string sql = "";
            if (Parent == 0)
            {
                sel = " DISTINCT enum_Parent_ID ";
                sql += " select * from ss_enum where  enum_Parent_ID in ( ";
            }

            sql += " select "+ sel + " from ss_enum where enumID in ( ";
            sql += " select substring_index(substring_index(a.enumID,',',b.help_topic_id),',',-1) ";
            sql += " from ";
            sql += " (select c.enumID from admin_user a ";
            sql += " left join admin_user_role b on a.userID = b.userID ";
            sql += " left join admin_role_permission c  on b.roleID = c.roleID ";
            sql += " where a.userID = '"+userid+"') a ) join help_topic b ";
            sql += "on b.help_topic_id < (length(a.enumID) - length(replace(a.enumID, ',', '')) + 2))";

            if (Parent == 0)
            {
               
                sql += "  ) ";
            }

            var connection = new MySqlConnection(ConStr);
            var command = new MySqlCommand
            {
                Connection = connection,
                CommandText = sql,
                CommandType = CommandType.Text
            };
            try
            {
                connection.Open();
                using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            retList.Add(new ss_enum()
                            {
                                enumID = Convert.ToInt32(reader["enumID"]),
                                enum_Parent_ID = Convert.ToInt32(reader["enum_Parent_ID"]),
                                enum_Seq = Convert.ToInt32(reader["enum_Seq"]),
                                enum_Number = reader["enum_Number"].ToString(),
                                enumName = reader["enumName"].ToString(),
                                enumTitle = reader["enumTitle"].ToString()
                            });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error("======================================================");
                _logger.Error("GetMenuByuserID ERROR !");
                _logger.Error("ERROR Message : " + ex.Message);
                _logger.Error("ERROR StackTrace : " + ex.StackTrace);
                _logger.Error("======================================================");
            }
            finally
            {
                connection.Close();
            }
            return retList;
        }


    }
}